package com.example.tab;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.tab.BookFragment;
import com.example.tab.MessageFragment;
import com.example.tab.MineFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private final Fragment messageFragment = new MessageFragment();

    private final Fragment bookFragment = new BookFragment();

    private final Fragment mineFragment = new MineFragment();
    private FragmentManager fragmentManager;
    private LinearLayout messageLayout,bookLayout,mineLayout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        messageLayout = findViewById(R.id.messageLayout);
        bookLayout = findViewById(R.id.bookLayout);
        mineLayout = findViewById(R.id.mineLayout);
        messageLayout.setOnClickListener(this);
        bookLayout.setOnClickListener(this);
        mineLayout.setOnClickListener(this);
        initFragment();

    }

    private void initFragment(){
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        messageLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
        bookLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
        mineLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
        transaction.add(R.id.frame_content,messageFragment);
        transaction.add(R.id.frame_content,bookFragment);
        transaction.add(R.id.frame_content,mineFragment);
        transaction.hide(bookFragment);
        transaction.hide(mineFragment);
        transaction.commit();
    }

    private void background(View v) {
        switch (v.getId()) {
            case R.id.messageLayout:
                messageLayout.setBackgroundColor(Color.parseColor("#007FFF"));
                break;
            case R.id.bookLayout:
                bookLayout.setBackgroundColor(Color.parseColor("#007FFF"));
                break;
            case R.id.mineLayout:
                mineLayout.setBackgroundColor(Color.parseColor("#007FFF"));
                break;
            default:
                break;
        }
    }

    private void backgroundReturn(View v) {
        switch (v.getId()) {
            case R.id.messageLayout:
                messageLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
                break;
            case R.id.bookLayout:
                bookLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
                break;
            case R.id.mineLayout:
                mineLayout.setBackgroundColor(Color.parseColor("#C1C1C1"));
                break;
            default:
                break;
        }
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(messageFragment);
        transaction.hide(bookFragment);
        transaction.hide(mineFragment);
    }

    private void showFragment(int i) {

        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(messageFragment);
                background(messageLayout);
                backgroundReturn(bookLayout);
                backgroundReturn(mineLayout);
                break;
            case 1:
                transaction.show(bookFragment);
                background(bookLayout);
                backgroundReturn(messageLayout);
                backgroundReturn(mineLayout);
                break;
            case 2:
                transaction.show(mineFragment);
                background(mineLayout);
                backgroundReturn(messageLayout);
                backgroundReturn(bookLayout);
                break;
            default:
                break;
        }
        transaction.commit();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.messageLayout:
                showFragment(0);
                break;
            case R.id.bookLayout:
                showFragment(1);
                break;
            case R.id.mineLayout:
                showFragment(2);
                break;
            default:
                break;
        }
    }
}
